#!/usr/bin/env python3
# encoding: utf-8

"""
@version: 1.0
@author: 周永波
@file: UserAction.py
@time: 2020/8/13 15:30
"""
import json

from flasgger import swag_from
from flask import request

from demoApp.doc import user_doc
from demoApp.models.baseInfo import User
from demoApp.setting.database import db
from demoApp.setting.es import esdb
from demoApp.setting.ret import views_rest
from demoApp.util.appUtils import dict_to_object

userAction = views_rest("userAction", __name__, url_prefix='/user')

@swag_from(user_doc.query)
@userAction.route("/insert", methods=['POST'])
def insert():
    reqPar = request.get_json()
    db.session.add(dict_to_object(reqPar, obj=User()))
    db.session.commit()
    return '插入成功'

@userAction.route("/query", methods=['get'])
# @swag_from(user_doc.query)
def query():
    name = request.args.get('name')
    id = request.args.get("id")
    # user = User.query.get(id=int(id))
    user = User.query.filter_by(userName='admin').first()
    # user = User.query.all()
    return user

@swag_from({})
@userAction.route("/esquery", methods=['get'])
def esQuery():
    # testData = esdb.get(index='test_es', id='', params={'password': 'cyq'})
    testData = esdb.search(index='test_es', body={
        'query':{
            'bool': {
                'must': {
                    'term': {
                        'password': 'cyq'
                    }
                }
        }
    }})
    return testData